import { deleteRightTodo, deleteRightItem, spliceGetDataTodo, getSaleNum, getValueArray, setEmptyArray, setArrayValue, setExchangeValue, spliceGetDataItem} from '@/views/sales/common/TableSaleMethods'
export default ({
  state: {
    form: {
      name: '',
      remarks: '',
      date: [],
      grade: [],
      term: [],
      num: "1",
      least: '',
      max: '',
      type: "1",
      slogan: ''
    },
    saleData: '',
    GetsaleData: ''
  },
  mutations: {
    setSingleSaleEmpty (state) {
      state.GetsaleData = '';
      state.saleData = '';
      state.form = {
        name: '',
        remarks: '',
        date: [],
        grade: [],
        term: [],
        num: "1",
        least: '',
        max: '',
        type: "1",
        slogan: ''
      }
    },
    deleteSingleData (state, object) {
      if(object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked){
        if(object.todo) { //没子集
            deleteRightTodo(object.item, object.todo, state.GetsaleData)
        }else { //有子集
            deleteRightItem(object.item, state.GetsaleData)
        }
      }  
    },
    getSingleData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
        index: type,
        childName: "productSkuList",
        status: false
      });
      setEmptyArray(getdata, {
        index: type,
        childName: "productSkuList"
      });
      setArrayValue(getdata, {
        index: type,
        val1: false,
        childName: "productSkuList"
      });
      if(!state.GetsaleData.length) { //首次为空
        state.GetsaleData = getdata
      } else { // 防止每次更新的淹没其他的
          //把没有的第一层数据存在，GetsaleData里面
          //把没有的第二层的数据存在, GetsaleData里面
          spliceGetDataItem(getdata, state.GetsaleData)
          spliceGetDataTodo(getdata, state.GetsaleData)
      }
      if (type !== "buttonChecked") {
        setExchangeValue(state.saleData, {
          index1: "checked",
          index: "buttonChecked",
          childName: "productSkuList"
        });
      }
    },
    validateSingleTerm(state, object) {
      if (state.form.type == 1) {
        object.callback()
      } else if (state.form.type == 2) { // 限时抢购
        if (!object.value.length) {
          return object.callback(new Error("选择限时抢购限购条件必须选择"))
        } else {
          object.callback()
        }
      }
    },
    validateSingleLeast(state, object) {
      if (state.form.num == '2') { //限时抢购
        if (!object.value) {
          return object.callback(new Error("请输入数量"))
        }else if (state.form.max && Number(object.value) > Number(state.form.max)) {
          return object.callback(new Error("不能大于最多"))
        } else {
          object.callback()
        }
      } else {
        object.callback()
      }
    },
    validateSingleMax(state, object) {
      if (state.form.num == '2') { //限时抢购
        if (!object.value) {
          return object.callback(new Error("请输入数量"))
        }else if (Number(state.form.least && object.value) < Number(state.form.least)) {
          return object.callback(new Error("不能小于最最少"))
        } else {
          object.callback()
        }
      } else {
        object.callback()
      }
    }
  },
  getters: {
    // 是不是直降 - 直降有限购数量 -其他没有（默认为直降）
    isShowDrop(state) {
      return state.form.type !== "1" ? true : false 
    },
    getSingleSaleNum(state) {
      return getSaleNum(state.GetsaleData, {
        childName: 'productSkuList'
      })
    },
    singletOneOrderTypeEnum(state) {
      if (state.form.term[0] == '2') {
        return 'ACCOUNT'
      } else {
        return 'ALL_NOT'
      }
    },
    singleLimitOneOrder(state) {
      if (state.form.type == 1 && state.form.term[0] !== 2) {
        return false
      } else {
        return true
      }
    },
    singleLimitQuantity(state) {
      if (state.form.num == 1) { //直降
        return false
      } else {
        return true
      }
    },
    singlelimitLeast(state) {
      let word = ''
      if (state.form.num == '1') {
        word = ""
      } else if (state.form.num == '2') {
        word = state.form.least
      }
      return word
    },
    singlelimitMax(state) {
      let word = ''
      if (state.form.num == '1') {
        word = ""
      } else if (state.form.num == '2') {
        word = state.form.max
      }
      return word
    },
    singleSaleType(state) {
      if (state.form.type == 1) {
        return 'PRICE_REDUCTION'
      } else if (state.form.type == 2) {
        return 'TIME_LIMIT'
      }
    },
    singleNum(state) {
      if (state.form.num == 1) {
        return true;
      } else {
        return false;
      }
    },
    singleGrade(state, getters) {
      let newarray = []
      for (let item of state.form.grade) {
        for (let todo of getters.storeTypeData) {
          if (item == todo.value) {
            newarray.push(todo.text)
          }
        }
      }
      return newarray.join(',')
    },
    singleType(state) {
      if (state.form.type == 1) {
        return '直降'
      } else if (state.form.type == 2) {
        return '限时抢购'
      }
    },
    hasSingleMinMax(state) {
      if (state.form.num == '1') {
        return true
      } else {
        return false;
      }
    }
  }
})